package com.wisdom.springbootdemo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * Created with IntelliJ IDEA.
 * Description:请求，传递参数
 * User: 11951
 * Date: 2025-03-01
 * Time: 17:11
 */
@RequestMapping("/param")
@RestController
public class ParamController {
    //    1.传递单个参数
    @RequestMapping("/m1")
    public String method1(String name) {
        return "接收到参数name:" + name;
    }

    //    2.传递多个参数
    @RequestMapping("/m2")
    public String method2(String name, int age) {
        return "接收到参数name：" + name + ",接收到参数age:" + age;
    }

    //    3.传递对象
    @RequestMapping("/m3")
    public Object Person(Person person) {
        return person.toString();
    }

    //    4.后端参数重命名
    @RequestMapping("/m4")
    public Object method4(@RequestParam("time") String createtime) {
        return "接收到的createtime:" + createtime;
    }

    //    ⾮必传参数设置
    @RequestMapping("/m5")
    public Object method5(@RequestParam(value = "time", required = false) String createtime) {
        return "接收到的createtime:" + createtime;

    }

    //5.传递数组
    @RequestMapping("/m6")
    public String method6(String[] array) {
        return Arrays.toString(array);
    }

    //    6.传递集合
    @RequestMapping("/m7")
    public String method7(@RequestParam List<String> listParam) {
        return "size:" + listParam.size() + "/listParam:" + listParam;
    }

    //    7.传递JSON数据
    @RequestMapping("/m8")
    public Object method8(@RequestBody Person person) {
        return person.toString();
    }

    //    尝试去除掉 @RequestBody
    @RequestMapping("/m9")
    public Object method9(Person person) {
        return person.toString();
    }

    //    8.获取URL中参数@PathVariable
    @RequestMapping("/m10/{id}/{name}")
    public String method10(@PathVariable Integer id, @PathVariable("name") String userName) {
        return "解析ID:" + id + ",name:" + userName;
    }

    //    9.上传⽂件@RequestPart
    @RequestMapping("/m11")
    public String getfile(@RequestPart("file") MultipartFile file) throws IOException {
        //获取文件名称
        String fileName = file.getOriginalFilename();
        //将文件上传到指定路径
        file.transferTo(new File("D:/MyPhotos/" + file.getOriginalFilename()));
        return "接收到文件名称为：" + fileName;
    }

//    10.获取Cookie
//            传统获取Cookie
        @RequestMapping("/m12")
    public String method12(HttpServletRequest request, HttpServletResponse response) {
        //获取所有cookie信息
        Cookie[] cookies = request.getCookies();
        //打印cookie信息
        StringBuilder builder = new StringBuilder();
        if (cookies != null) {
            for (Cookie ck : cookies) {
                builder.append(ck.getName() + ":" + ck.getValue());
            }
        }
        return "Cookie信息："+builder;
    }

//    简洁获取Cookie
        @RequestMapping("/getCookies")
    public String getCookies(@CookieValue("name") String name){
        return "name:"+name;
    }

//    11.获取session
//    1.session存储
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        //获取session对象
        HttpSession session= request.getSession();
        if(session!=null){
            session.setAttribute("username","java");
        }
        return "session存储成功";
    }
//    2.session获取method1
    @RequestMapping("/getSession1")
    public  String getSession1(HttpServletRequest request){
        // 如果 session 不存在, 不会⾃动创建
        HttpSession session= request.getSession(false);
        String username =null;
        if(session != null && session.getAttribute("username")!=null){
            username=(String) session.getAttribute("username");
        }
        return  "username:"+username;
    }
//    method2
    @RequestMapping("/getSession2")
    public  String getSession2(@SessionAttribute(value = "username",required = false)String userName){
        return "username:"+userName;
    }
//    method3
    @RequestMapping("/getSession3")
    public  String getSession3(HttpSession session){
        String username=(String) session.getAttribute("username");
        return "username:"+username;
    }

//    12.获取Header
//    method1
@RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request,HttpServletResponse response){
        String userAgent=request.getHeader("User-Agent");
         return "useragent:"+userAgent;
    }

//    method2
    @RequestMapping("/header")
    public String header(@RequestHeader("User-Agent") String useragent){
        return "useragent:"+useragent;
    }


}


